//
//  CXDestinationViewController.m
//  XiMaFM
//
//  Created by hcios on 2017/9/6.
//  Copyright © 2017年 hcios. All rights reserved.
//

#import "CXDestinationViewController.h"
#import "CXTracksViewModel.h"
#import "CXDestinationTableViewCell.h"
#import "CXZJDetailView.h"

@interface CXDestinationViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) CXTracksViewModel * destinationModel;

@property (nonatomic, assign) BOOL isAsc;

@property (nonatomic, strong) CXZJDetailView * detailView;

@end

@implementation CXDestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.destinationModel getDataCompletionHandle:^(NSError *error) {
        
        [_detailView.imageIcon setImageWithURL:self.destinationModel.albumIconURL];
        _detailView.titleLabel.text = self.destinationModel.albumTitle;
        [self.tableView reloadData];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    [self tableView];
    [self detailView];
}



-(instancetype)initWithAlbumId:(NSInteger)albumId title:(NSString *)oTitle {
    if (self = [super init]) {
        _albumId = albumId;
        _oTitle = oTitle;
    }
    return self;
}

//视图即将消失
-(void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = NO;
    
}


#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.destinationModel.rowNumber;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CXDestinationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"DestinationCell"];
    
    [cell.coverImageView setImageWithURL:[self.destinationModel coverURLWithRow:indexPath.row] placeholderImage:kGetImage(@"album_cover_bg")];
    cell.titleLabel.text = [self.destinationModel titleWithRow:indexPath.row];
    cell.timeLabel.text = [self.destinationModel updateTimeWithRow:indexPath.row];
    [cell.playButton setTitle:[self.destinationModel playsCountWithRow:indexPath.row] forState:UIControlStateNormal];
    [cell.durationButton setTitle:[self.destinationModel playTimeWithRow:indexPath.row] forState:UIControlStateNormal];
    [cell.commentButton setTitle:[self.destinationModel commentCountWithRow:indexPath.row] forState:UIControlStateNormal];
    
    
    cell.backgroundColor = [UIColor colorWithRed:239 / 255.0  green:243 / 255.0  blue:247 / 255.0 alpha:1];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //通知旋转按钮,播放及按钮改变
    NSMutableDictionary * userInfo = [NSMutableDictionary dictionary];
    userInfo[@"coverURL"] = [self.destinationModel coverURLWithRow:indexPath.row];
    userInfo[@"musicURL"] = [self.destinationModel playURLWithRow:indexPath.row];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Beginplay" object:nil userInfo:[userInfo copy]];
    
}

#pragma mark - 懒加载
-(CXTracksViewModel *)destinationModel{
    if (!_destinationModel) {
        _destinationModel = [[CXTracksViewModel alloc] initWithAlbumId:_albumId title:_oTitle isAsc:!_isAsc];
        
    }
    return _destinationModel;
}


-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CXScreenWidth, CXScreenHeight) style:UITableViewStylePlain];
        [self.view addSubview:_tableView];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [_tableView registerNib:[UINib nibWithNibName:@"CXDestinationTableViewCell" bundle:nil] forCellReuseIdentifier:@"DestinationCell"];
        
        
        // 上拉刷新
        _tableView.mj_header = [CXRefreshStateHeader headerWithRefreshingBlock:^{
            [self.destinationModel getDataCompletionHandle:^(NSError *error) {
                [self.tableView.mj_header endRefreshing];
                [self.tableView reloadData];
            }];
        }];
        // 下拉加载数据
        _tableView.mj_footer = [CXRefreshAutoStateFooter footerWithRefreshingBlock:^{
            [self.destinationModel getDataCompletionHandle:^(NSError *error) {
                [_tableView.mj_footer endRefreshing];
                [_tableView reloadData];
            }];
        }];
        
        _tableView.rowHeight = 100;
        
    }
    return _tableView;
}



@end
